package com.jhc.drone.communication.network.router;


import com.jhc.drone.communication.properties.ProtocolProperties;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author lcp
 * @date 2025年01月2025/1/17 9:17
 * @describe
 */
public class UdpRouter {

    public static void start(ProtocolProperties protocolProperties) {
        try {
            Integer routerGoalPort = protocolProperties.routerGoalPort;
            Integer port = protocolProperties.port;
            Integer qgcPort = protocolProperties.qgcPort;
            Integer routerLocalPort = protocolProperties.routerLocalPort;
            String qgcAddress = protocolProperties.qgcAddress;
            String serverAddress = protocolProperties.serverAddress;

            DatagramSocket server1Socket = new DatagramSocket(routerGoalPort);
            DatagramSocket serverSocket7901 = new DatagramSocket();
            DatagramSocket serverSocket9999 = new DatagramSocket();
            DatagramSocket serverSocket8600 = new DatagramSocket();
            System.out.println("UDP Server1 is listening on port 8888...");
            InetAddress serverIp = InetAddress.getByName(serverAddress);
            InetAddress qgcIp = InetAddress.getByName(qgcAddress);
            byte[] receiveData = new byte[1024];
            byte[] receiveData9999 = new byte[1024];

            new Thread(() -> {
                while (true) {
                    try {
                        // 1. 接收来自client1和client2的消息
                        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                        server1Socket.receive(receivePacket);
                        String receivedMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
                        //System.out.println("Received message from client: " + receivedMessage);

                        // 2. 转发消息给client2
                        DatagramPacket forwardToClient2 = new DatagramPacket(receivePacket.getData(), receivePacket.getLength(), qgcIp, qgcPort);
                        serverSocket9999.send(forwardToClient2);
                        //System.out.println("Forwarded message to client2.");

                        // 3. 转发消息给UDP Server2
                        DatagramPacket forwardToServer2 = new DatagramPacket(receivePacket.getData(), receivePacket.getLength(), serverIp, port);
                        serverSocket7901.send(forwardToServer2);
                        //System.out.println("Forwarded message to UDP Server2.");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            // 4. 接收UDP Server2的消息并转发给client1和client2
            new Thread(() -> {
                while (true) {
                    try {
                        DatagramPacket server2Response = new DatagramPacket(receiveData, receiveData.length);
                        serverSocket7901.receive(server2Response);
                        String server2Message = new String(server2Response.getData(), 0, server2Response.getLength());

                        // 转发给client1
                        DatagramPacket forwardToClient1 = new DatagramPacket(server2Response.getData(), server2Response.getLength(), qgcIp, routerLocalPort);
                        serverSocket8600.send(forwardToClient1);
                        //System.out.println("Forwarded message from Server2 to client1.");

                        //System.out.println("Forwarded message from Server2 to client2.");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            new Thread(() -> {
                while (true) {
                    try {

                        // 1. 接收来自client1和client2的消息
                        DatagramPacket receivePacket = new DatagramPacket(receiveData9999, receiveData9999.length);
                        serverSocket9999.receive(receivePacket);
                        String receivedMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
//                        System.out.println("Received message from client: " + receivedMessage);

                        // 2. 转发消息给client2
                        DatagramPacket forwardToClient2 = new DatagramPacket(receivePacket.getData(), receivePacket.getLength(), qgcIp, routerLocalPort);
                        serverSocket8600.send(forwardToClient2);

//                        System.out.println("Forwarded message to client2.");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
